﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace BlowFish
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label4.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (richTextBox2.Text.Length > 1 && richTextBox1.Text != "")
            {
                label4.Text ="";
                BlowFish b = new BlowFish(Encoding.ASCII.GetBytes(richTextBox2.Text));
                string plainText = richTextBox1.Text;
                richTextBox3.Text = b.Encrypt_CBC(plainText);
                //plainText = b.Decrypt_CBC(cipherText);
               // MessageBox.Show(plainText);
            }
            else
            {
                label4.Text = "Вы забыли ввести данные?";
            }
        }


        private void button5_Click(object sender, EventArgs e)
        {
            List<byte> lkey = new List<byte>();
            // генерируем 8 случайных больших неотрицательных чисел
            for (int i = 0; i <= 7; i++)
            {
                Random rndgen = new Random();
                uint k = (uint)rndgen.Next(int.MaxValue);
                lkey.AddRange(BitConverter.GetBytes(k));
                System.Threading.Thread.Sleep(10);
            }
            richTextBox2.Text = ToStringFromByteArray(lkey.ToArray());
            if (Encoding.ASCII.GetByteCount(richTextBox2.Text) * 8 < 256)
            {
                button5_Click(sender, e);
            }
            
                 
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
            if (richTextBox2.Text.Length > 0)
            {
                label3.Text = Encoding.ASCII.GetByteCount(richTextBox2.Text)*8 + " bits";
            }
        }

        public string ToStringFromByteArray(byte[] val)
        {
            char[] chars = new char[val.Length];
            string result = "";
            for (int i = 0; i < val.Length; i++)
            {
                chars[i] = Convert.ToChar(val[i]);
                result += chars[i];
            }
            return result;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "key";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string pathtofile = this.openFileDialog1.FileName;
                FileStream fs = new FileStream(pathtofile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bt = br.ReadBytes((int)fs.Length);
                richTextBox2.Text = System.Text.Encoding.UTF8.GetString(bt);              
            }

            openFileDialog1.FileName = "message";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string pathtofile = this.openFileDialog1.FileName;
                FileStream fs = new FileStream(pathtofile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bt = br.ReadBytes((int)fs.Length);
                richTextBox1.Text = System.Text.Encoding.UTF8.GetString(bt);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.FileName = "key";
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    myStream.Write(Encoding.ASCII.GetBytes(richTextBox2.Text), 0, Encoding.ASCII.GetByteCount(richTextBox2.Text));
                    myStream.Close();
                }
            }

            saveFileDialog1.FileName = "message";
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    myStream.Write(Encoding.ASCII.GetBytes(richTextBox3.Text), 0, Encoding.ASCII.GetByteCount(richTextBox3.Text));
                    myStream.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (richTextBox2.Text.Length > 1 && richTextBox1.Text != "")
            {
                label4.Text = "";
                BlowFish b = new BlowFish(Encoding.ASCII.GetBytes(richTextBox2.Text));
                string plainText = richTextBox1.Text;
                richTextBox3.Text = b.Decrypt_CBC(plainText);
                //plainText = b.Decrypt_CBC(cipherText);
                // MessageBox.Show(plainText);
            }
            else
            {
                label4.Text = "Вы забыли ввести данные?";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            richTextBox2.Text = "";
            richTextBox3.Text = "";
        }


    }
}
